package com.dqc.qlibrary.utils;

import android.app.Service;
import android.os.VibrationEffect;
import android.os.Vibrator;

import com.dqc.qlibrary.QLibrary;

/**
 * Android 手机震动工具类
 * <p>
 * 需要权限【android.permission.VIBRATE】
 *
 * @author .
 */
public class VibratorUtils {

    /**
     * 创建一个一次性振动
     *
     * @param milliseconds 震动的时长，单位是毫秒
     */
    public static void vibrate(long milliseconds) {
        Vibrator vib = (Vibrator) QLibrary.getInstance().getContext().getSystemService(Service.VIBRATOR_SERVICE);
        if (vib == null) {
            return;
        }
        vib.vibrate(VibrationEffect.createOneShot(milliseconds, VibrationEffect.DEFAULT_AMPLITUDE));
    }

    /**
     * 自定义一个 VibrationEffect 震动
     *
     * @param vibe VibrationEffect
     */
    public static void vibrate(VibrationEffect vibe) {
        Vibrator vib = (Vibrator) QLibrary.getInstance().getContext().getSystemService(Service.VIBRATOR_SERVICE);
        if (vib == null) {
            return;
        }
        vib.vibrate(vibe);
    }

}  